<?php
include_once '!config.php';
use models\Form as Form;
use models\Menu as Menu;
use models\User as User;
use models\Table as Table;
use models\Relation as Relation;


use helpers\Helper as Helper;
if(isset($_SESSION['user'])){
    if(!isset($_GET['type'])){
        $_GET['type'] = 'menu';
        $_GET['action'] = 'tabOpen';
        $_GET['tab'] = 1;
    }
    switch($_GET['type']){


    case 'user'     :   
                       
                        $user = new User();


                        switch($_GET['action'])
                        {
                            case 'logOut'    :  unset($_SESSION['user']);
                                                $smarty->display('signin.tpl');
                                                break;
                        }


                        break;


    case 'menu'     :   
                        switch($_GET['action'])
                        {
                            case 'tabOpen'    :     unset($_SESSION['query']);
                                                    unset($_SESSION['navigation']);

                                                    $menuObj = new Menu();
                                                    $menuObj->getMenu($_GET['tab']);
                                                    $smarty->assign('menu', $menuObj->menu);
                                                    $_SESSION['navigation']['menu'] = $_GET['tab'];
                                                    $table = new Table();
                                                    $form = new Form();
                                                    $smarty->assign('tableHeaders', Table::getTableHeaders($_GET['tab']));
                                                    $table->setLimit(0);
                                                    $smarty->assign('tableRows', $table->getTableRows($_GET['tab']));
                                                    $smarty->assign('pagesCount', $table->getPagesCount());
                                                    $smarty->assign('formRights', $form->getFormRights($_GET['tab']));
                                                    $smarty->display('main.tpl');
                                                    break;
                                                
                            case 'setLanguage':     setcookie('CurrantLanguage', $_GET['lang'], time()+60*60*24*31*6);
                                                    header("Location: $_SERVER[PHP_SELF]");
                                                    break;
                        }


                        break;


    case 'table'    :   $table = new Table();
                        $form = new Form();
                        switch ($_GET['action']) {


                            case 'setTableWhere'    :   $relation = new Relation($_SESSION['navigation']['menu']);
                                                        $relation->where($_POST);
                                                        break;


                            case 'setTableOrder'    :   $table->setOrder($_POST['fieldId']);
                                                        break;


                            case 'setRowsPerPage'   :   $table->setRowsPerPage($_POST['rowsPerPage']);
                                                        break;

                                                    
                            case 'setLimit'         :   $table->setLimit($_POST['page']);
                                                        break;

                            case 'getPagesCount'    :   $smarty->assign('pagesCount', $table->getPagesCount());
                                                        $smarty->display('paging.tpl');
                                                        exit;

                                                        
                            case 'setSequence'      :   $table->setSequence($_SESSION['query']['from'], $_POST['sequence']);
                                                        exit;


                        }
                        $smarty->assign('formRights', $form->getFormRights());
                        $smarty->assign('tableRows', $table->getTableRows());
                        $smarty->display('table/rows.tpl');
                        break;


    case 'form'    :    require_once 'models/Form.class.php';
                        $form = new Form();
                        switch ($_GET['action']) {

                            case 'open'         :   if(isset($_GET['id'])) $smarty->assign('id', $_GET['id']);
                                                    $smarty->assign('formRights', $form->getFormRights());
                                                    $rows = $form->getFormRows($_GET['id']);
                                                    $smarty->assign('rows', $rows);
                                                    $smarty->display('form/form.tpl');
                                                    break;

                            case 'close'        :   $tab = $_SESSION['navigation']['menu'];
                                                    header("Location: $_SERVER[PHP_SELF]?type=menu&action=tabOpen&tab=$tab");
                                                    break;

                            case 'update'       :   $form->update($_POST);
                                                    break;
                                                
                            case 'create'       :   $form->create($_POST);
                                                    break;

                            case 'delete'       :   $form->delete($_POST['id']);
                                                    break;                                                

                            case 'multiDelete'  :   $form->multiDelete($_POST['id']);
                                                    break;

                        }
                        break;

    case 'helper'   :   switch ($_GET['action'])
                        {
                            case 'createNewItem':   echo json_encode(Helper::createNewItem($_SESSION['query']['from']));
                                                    break;
                        }
                        break;
                    

    }
}
else {
    if($_GET['type'] == 'user' and $_GET['action'] == 'logIn'){
        $user = new User();
        if($user->signIn($_POST['email'], $_POST['password'])){
            $menuObj = new Menu();
            $menuObj->getMenu(1);
            $smarty->assign('menu', $menuObj->menu);
            $_SESSION['navigation']['menu'] = 1;
            $smarty->display('main.tpl');
        }
        else $smarty->display('signin.tpl');
    }
    else $smarty->display('signin.tpl');
}
?>